package com.yuniarti.konsultasikesehatan;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

import android.widget.Toast;

public class DaftarActivity extends Activity {

	EditText un, pw, nl, tl, al;
	RadioGroup jk, gd;
	
	Button simpan, keluar;
	
	DataParser jsonParser = new DataParser();
	private static String daftar_member = "http://konsultasi-yakes.web.id/JSON/daftar-member.php";
	//private static String daftar_member = "http://10.0.2.2/adminweb/JSON/daftar-member.php";
	private static final String TAG_HASIL = "hasil";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_daftar);
		
		un = (EditText) findViewById(R.id.idMember);
		pw = (EditText) findViewById(R.id.password);
		nl = (EditText) findViewById(R.id.namalengkap);
		tl = (EditText) findViewById(R.id.tanggallahir);
		al = (EditText) findViewById(R.id.alamat);
		jk = (RadioGroup) findViewById(R.id.jekel);
		gd = (RadioGroup) findViewById(R.id.gol_darah);
		
		
		simpan =(Button) findViewById(R.id.btn_simpan);
		keluar =(Button) findViewById(R.id.btn_keluar);
		
		simpan.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				
				String id_member = un.getText().toString();
	            String password = pw.getText().toString();
	            String namalengkap = nl.getText().toString();
	            String tanggallahir = tl.getText().toString();
	            String alamat = al.getText().toString();
	            
	            String type = null;
				switch (jk.getCheckedRadioButtonId()) {
				case R.id.pria:
					type="Pria";
					break;
				case R.id.wanita:
					type="Wanita";
					break;
				default:
					break;
				}
				
				String gol = null;
				switch (gd.getCheckedRadioButtonId()) {
				case R.id.A:
					gol="A";
					break;
				case R.id.B:
					gol="B";
					break;
				case R.id.O:
					gol="O";
					break;
				case R.id.AB:
					gol="AB";
					break;
				default:
					break;
				}

					List<NameValuePair> params = new ArrayList<NameValuePair>();
		            params.add(new BasicNameValuePair("id_member", id_member));
		            params.add(new BasicNameValuePair("password", password));
		            params.add(new BasicNameValuePair("namalengkap", namalengkap));
		            params.add(new BasicNameValuePair("tanggallahir", tanggallahir));
		            params.add(new BasicNameValuePair("jekel", type));
		            params.add(new BasicNameValuePair("alamat", alamat));
		            params.add(new BasicNameValuePair("gol", gol));

		            JSONObject json = jsonParser.makeHttpRequest(daftar_member,"POST", params);
		         
		        
		            try {
		            	
		            	int hasil = json.getInt(TAG_HASIL);
		            	 
		                if (hasil == 1) {

		                	Toast.makeText(DaftarActivity.this, "Input Berhasil YESSS!", Toast.LENGTH_LONG).show();

		                	Intent i = new Intent(getApplicationContext(), LoginActivity.class);
		                    startActivity(i);
		                    finish();
		                    
		                }
		                else {
		                	
		                	Toast.makeText(DaftarActivity.this, "Input Gagal!", Toast.LENGTH_SHORT).show();
		                //	txt_username.setText("Login Gagal");       
		                }
		            	
		                
		            } catch (JSONException e) {
		                e.printStackTrace();
		            }
				
			}
		});
    }
}
